﻿<%@ WebHandler Language="C#" Class="MfgorderContainerProductFamilyHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Data;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using BusinessRule.DTO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;
using DatabaseAccess.DBUtility;
using Camstar.WebClient.FormsBuilder.WebForm;
using Camstar.WebClient.WebServicesProxy.InSiteWebServices;
using External.APICalling;

public class MfgorderContainerProductFamilyHandler : IHttpHandler, IRequiresSessionState
{

    CurrentUserInfo m_UserInfo;
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            switch (action)
            {
                case "GetMfgorderContainerProductFamily"://查询产品加工试样信息
                    GetMfgorderContainerProductFamily(context);
                    break;
                case "SaveMfgorderContainerProductFamily"://
                    SaveMfgorderContainerProductFamily(context);
                    break;
                case"GetWayCallbackWMSData":
                    GetWayCallbackWMSData(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

    void GetMfgorderContainerProductFamily(HttpContext context)
    {
        var paraData = JsonConvert.DeserializeObject<Dictionary<string, string>>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var selectSQL = @"SELECT m.MFGORDERNAME,m.PRODUCTFAMILYNAME,m.ZHBMODEL,
                    CASE WHEN m.ZHBMODEL IS NULL THEN m.PRODUCTFAMILYNAME ELSE m.PRODUCTFAMILYNAME||'/'||m.ZHBMODEL END MFAMILYLISTNAMES,
                    pb.PRODUCTNAME||':'||p.PRODUCTREVISION PRODUCTNAME,p.DESCRIPTION
                    ,cci.CONTAINERID,cci.CONTAINERNAME
                    ,CASE WHEN m.ZHBMODEL IS NULL THEN cci.FAMILYLISTNAMES ELSE cci.FAMILYLISTNAMES||'/'||m.ZHBMODEL END FAMILYLISTNAMES
                    ,decode(cci.CONTAINERTYPE,2,'批量加工'，3,'批量带子序号',7,'单件加工','未知') CONTAINERTYPE
                    --,decode(cci.CONTAINERSTATUS,0,'在制中',1,'完工入库',2,'关闭',-1,'领料中','未知' )CONTAINERSTATUS
                    ,CASE WHEN cci.ISINSTORE =1 THEN '完工入库'
                    	  --WHEN cci.ISINSTORE =-1 THEN '领料中'
                    	  ELSE decode(cci.CONTAINERSTATUS,1,'在制中',2,'已关闭','') 
                   	END CONTAINERSTATUS
                    FROM MFGORDER m
                    LEFT JOIN PRODUCTBASE pb1 ON pb1.PRODUCTBASEID =m.PRODUCTBASEID 
                    LEFT JOIN PRODUCT p ON p.PRODUCTID = NVL(PB1.REVOFRCDID,m.PRODUCTID)
                    LEFT JOIN PRODUCTBASE pb ON pb.PRODUCTBASEID =p.PRODUCTBASEID 
                    inner JOIN CONTAINERCURRENTINFO cci ON cci.MFGORDERID =m.MFGORDERID 
                    WHERE 1=1 ";
        selectSQL += " and cci.originalfactoryid='"+m_UserInfo.FactoryID+"'";
        if (!string.IsNullOrWhiteSpace(paraData["ProductName"]))
        {
            selectSQL += " and pb.ProductName like '%" + paraData["ProductName"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["ContainerName"]))
        {
            selectSQL += " and cci.ContainerName like '%" + paraData["ContainerName"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["MfgorderName"]))
        {
            selectSQL += " and m.MfgorderName like '%" + paraData["MfgorderName"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["ProductFamily"]))
        {
            selectSQL += " and m.PRODUCTFAMILYNAME like '%" + paraData["ProductFamily"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["ContaineStatus"]))
        {
            if (paraData["ContaineStatus"].Equals("-1")||paraData["ContaineStatus"].Equals("3")) 
            {
                selectSQL += " and cci.isinstore='"+(paraData["ContaineStatus"].Equals("3")?"1":"-1")+"'";
            }
            if (paraData["ContaineStatus"].Equals("1") || paraData["ContaineStatus"].Equals("2"))
            {
                selectSQL += " and nvl(cci.isinstore,0)=0 and cci.CONTAINERSTATUS='" + paraData["ContaineStatus"] + "'";
            }
        }
        if (!string.IsNullOrWhiteSpace(paraData["mfgStartDate"]))
        {
            selectSQL += " and m.PlannedStartDate>=to_date('" + paraData["mfgStartDate"] + "','yyyy-MM-dd')";
        }

        if (!string.IsNullOrWhiteSpace(paraData["mfgEndDate"]))
        {
            selectSQL += " and m.PlannedStartDate<=to_date('" + paraData["mfgEndDate"] + "','yyyy-MM-dd')";
        }
        
        
        selectSQL += "order by m.MfgorderName desc ";
        var paraPage = new uMESPagingDataDTO();
        paraPage.CurrentPageIndex = Convert.ToInt32(pageNumber);
        paraPage.PageSize = Convert.ToInt32(pageSize);
        paraPage.strSQL = selectSQL;
        var result = DatabaseHelper.RunProcedureForPage(paraPage);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    void SaveMfgorderContainerProductFamily(HttpContext context)
    {
        var paraDT = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        var result = new uMESProcessResult();
        var updateSQL = @"UPDATE CONTAINERCURRENTINFO cci SET cci.FAMILYLISTNAMES ='{1}',cci.FAMILYLISTIDS =(SELECT  wm_concat(p.PRODUCTFAMILYID) FROM PRODUCTFAMILY p WHERE p.PRODUCTFAMILYNAME IN ('{2}'))
                        WHERE cci.CONTAINERID ='{0}'";
        var updateSQL2 = @"UPDATE CONTAINER c set c.MFGBATCH='{1}' WHERE c.CONTAINERID='{0}'";
        var arrList = new ArrayList();
        foreach(DataRow row in paraDT.Rows)
        {
            if (!string.IsNullOrWhiteSpace(row["PRODUCTFAMILYNAME"].ToString()))
            {
                arrList.Add(string.Format(updateSQL, row["containerid"],row["PRODUCTFAMILYNAME"],row["PRODUCTFAMILYNAME"].ToString().Replace(";","','")));
                arrList.Add(string.Format(updateSQL2, row["containerid"], row["PRODUCTFAMILYNAME"]));
            }
        }
        if (arrList.Count>0) 
        {
            result.Result = DatabaseHelper.ExecuteSqlListTran(arrList);
        }
        result.Message = result.Result ? "更新成功" : "更新失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    void GetWayCallbackWMSData(HttpContext context)
    {
        var paraData = JsonConvert.DeserializeObject<Dictionary<string, string>>(context.Request["data"].ToString());
        string pageSize = context.Request["pageSize"].ToString();
        string pageNumber = context.Request["pageNumber"].ToString();
        var selectSQL = @"SELECT * FROM Way_Callback_WMS way 
                    WHERE 1=1 ";
        if (!string.IsNullOrWhiteSpace(paraData["MfgorderName"]))
        {
            selectSQL += " and way.MfgorderName like '%" + paraData["MfgorderName"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["ContainerName"]))
        {
            selectSQL += " and way.batchnum like '%" + paraData["ContainerName"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["WaybillNo"]))
        {
            selectSQL += " and way.way_no like '%" + paraData["WaybillNo"] + "%'";
        }
        if (!string.IsNullOrWhiteSpace(paraData["waybillStatus"]))
        {
            selectSQL += " and way.way_Status ='" + paraData["waybillStatus"] + "'";
        }
        selectSQL += "order by way.MfgorderName desc ";
        var paraPage = new uMESPagingDataDTO();
        paraPage.CurrentPageIndex = Convert.ToInt32(pageNumber);
        paraPage.PageSize = Convert.ToInt32(pageSize);
        paraPage.strSQL = selectSQL;
        var result = DatabaseHelper.RunProcedureForPage(paraPage);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }
}